#!/usr/bin/env ruby

module RedBook
	TEST_HOME = File.dirname(File.expand_path(__FILE__))+'/..' unless const_defined?(:TEST_HOME)
end

require "#{RedBook::TEST_HOME}/lib/redbook"



describe RedBook::HookCollection do

	before :each do
		@hooks = RedBook::HookCollection.new
	end

  it "should create a new hook when sent #add" do
    @hooks.add :test_hook do Math.sqrt(4) end
    (@hooks[:test_hook]/:hook).length.should == 1
  end
  
  it "should execute hook code when sent #exec" do
    result = 4
    @hooks.add :test_hook do result = Math.sqrt(result) end
    @hooks.add :test_hook2 do |n| result = 2*n end
    @hooks.add :test_hook2 do |n| result = 2*n*n; true end
    @hooks.add :test_hook2 do |n| result = 2*n*n*n end
    @hooks.exec(:test_hook)
    result.should == 2.0
    @hooks.exec(:test_hook2, 3).should == true
    result.should == 18.0 # should only execute the first two hooks
  end
  
end
